package com.phospherus.flow.service

import com.phospherus.flow.entity.BuyerLeftToTcf
import com.phospherus.flow.repository.BuyerLeftToTcfRepository
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * 流程6
 * @author songjinlong
 * @version 1.0
 * @since 2018/1/3
 */
@Service
class BuyerLeftToTcfService : FormInstBaseService() {
    @Autowired
    lateinit var repository: BuyerLeftToTcfRepository

    override fun getFormInstServiceName(): String = "buyerlefttotcf"

    override fun save(map: MutableMap<String, Any>) {
        var formInstId = map["formInstId"] as Long
        var buyerlefttotcf_bindFContracts = map["buyerlefttotcf_bindFContracts"] as String?
        var buyerlefttotcf_bindDContracts = map["buyerlefttotcf_bindDContracts"] as String?
        var id = (map["id"] as? String)?.toLong()

        var buyertotcf = repository.save(BuyerLeftToTcf(id = id, formInstId = formInstId, buyerlefttotcf_bindFContracts = buyerlefttotcf_bindFContracts, buyerlefttotcf_bindDContracts = buyerlefttotcf_bindDContracts))
        var files = map["attachedFiles"] as String?
        if (StringUtils.isNotBlank(files)) {
            attachedFileService.addFormInstAttachedFile(files, formInstId, "buyerlefttotcf", buyertotcf.id)
        }
    }

    override fun query(formInstId: Long): MutableMap<String, Any?> {
        var map = mutableMapOf<String, Any?>()
        var buyerLeftToTcf = repository.findByFormInstId(formInstId)
        map["buyerlefttotcf_bindFContracts"] = if (buyerLeftToTcf == null) {
            ""
        } else {
            buyerLeftToTcf.buyerlefttotcf_bindFContracts
        }
        map["buyerlefttotcf_bindDContracts"] = if (buyerLeftToTcf == null) {
            ""
        } else {
            buyerLeftToTcf.buyerlefttotcf_bindDContracts
        }
        map["id"] = if (buyerLeftToTcf == null) {
            ""
        } else {
            buyerLeftToTcf.id
        }
        map["attachedFileList"] = if (buyerLeftToTcf == null) {
            ""
        } else {
            attachedFileService.getFormAttachedFiles(formInstId, buyerLeftToTcf.id, "buyerlefttotcf")
        }
        return map
    }
}